﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.IO;
using System.Diagnostics;


namespace MDK
{
    public partial class MainForm : Form
    {

        private XDocument document = new XDocument();

        private MDK2Keil vsProj;

        private string FileName = "";
        private DefaultKeilRegPath KeilRegPath = new DefaultKeilRegPath();

        Option claOption = new Option();

        //private ElementHost elementHost;
        public MainForm()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //TargetListBOX.SelectedValueChanged += new System.EventHandler(TargetListBOX_SelectionChanged);
            base.AutoScaleMode = AutoScaleMode.None;
            toolStripButton_CreateVSProj.Enabled = false;
            claOption.ReadConfig();
            KeilRegPath.GetPath();
            if (KeilRegPath.UVPROJFILE == string.Empty && KeilRegPath.UVPROJXFILE == string.Empty)
            {
                MessageBox.Show("Keil installation path not found,please check your installation path.");
                base.Close();
            }
        }

        private void TargetListBox_Add(string[] Items)
        {
            listBox_Targets.Items.Clear();
            for (int i = 0; i < Items.Length; i++)
            {
                string newItem = Items[i];
                listBox_Targets.Items.Add(newItem);
            }
            listBox_Targets.SelectedIndex = 0;
        }

        private void GroupListBox_Add(string[] Items)
        {
            ListBox_Group.Items.Clear();
            for (int i = 0; i < Items.Length; i++)
            {
                string item = Items[i];
                ListBox_Group.Items.Add(item);
            }
            ListBox_Group.SelectedIndex = 0;
        }

        private void SrcFileBox_Add(string[] Items)
        {
            FileBox.Items.Clear();
            for (int i = 0; i < Items.Length; i++)
            {
                string item = Items[i];
                FileBox.Items.Add(item);
            }
        }

        private void TargetStatusBox_Add(string[] Str)
        {
            TargetStatus.Items.Clear();
            foreach (string item in Str)
            {
                if(item != null)
                {
                    TargetStatus.Items.Add(item);
                }
            }

        }

        private void TargetListBOX_SelectionChanged(object sander, EventArgs e)
        {
            if (listBox_Targets.SelectedIndex < 0)
            {
                return;
            }
            if (FileName != "")
            {
                string[] array = vsProj.MDK_TargetRead();
                string[] array2 = vsProj.MDK_GroupRead(array[listBox_Targets.SelectedIndex]);
                GroupListBox_Add(array2);
                string[] items = vsProj.MDK_SrcRead(array[listBox_Targets.SelectedIndex], array2[0]);
                SrcFileBox_Add(items);
                string[] str = vsProj.MDK_TargetStatusRead(array[listBox_Targets.SelectedIndex]);
                TargetStatusBox_Add(str);
            }
        }

        private void GroupListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (FileName != "")
            {
                string[] array = vsProj.MDK_TargetRead();
                string[] array2 = vsProj.MDK_GroupRead(array[listBox_Targets.SelectedIndex]);
                string[] items = vsProj.MDK_SrcRead(array[listBox_Targets.SelectedIndex], array2[ListBox_Group.SelectedIndex]);
                SrcFileBox_Add(items);
            }
        }

        private void FileBox_DoubleClick(object sander, EventArgs e)
        {
            if (FileBox.SelectedIndex < 0)
            {
                return;
            }
            string text = FileBox.SelectedItem.ToString();
            vsProj.OpenSrcFile(text);

        }

        private void SourcePathBOX_DoubleClick(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.DefaultExt = "uvproj";
            openFileDialog.Filter = "MDK Project File (*.uvprojx;*.uvproj)|*.uvprojx;*.uvproj|All files (*.*)|*.*";
            if (openFileDialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            string fileName = openFileDialog.FileName;

            OpenMDKProjFile(fileName);
        }


        void OpenMDKProjFile(string fileName)
        {
            if (fileName == "")
            {
                SourcePathBOX.Text = "Double-click or drag and drop The MDK the Project File here";
                SourcePathBOX.ForeColor = Color.Gray;
                FileName = "";
                toolStripButton_CreateVSProj.Enabled = false;
                return;
            }

            string uv4Path = KeilRegPath.GetMDKPath(fileName);
            vsProj = new MDK2Keil(fileName, uv4Path);

            if (MDK_Display_Info(fileName))
            {
                SourcePathBOX.ForeColor = Color.Black;
                SourcePathBOX.Text = fileName;
                FileName = fileName;
                vsProj.KeilProjFullName = fileName;
                return;
            }
            SourcePathBOX.Text = "Double-click or drag and drop The MDK the Project File to here";
            SourcePathBOX.ForeColor = Color.Gray;
            FileName = "";
            toolStripButton_CreateVSProj.Enabled = false;
        }


        private void SourcePathBOX_DragEnter(object sander, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Link;
                return;
            }
            e.Effect = DragDropEffects.None;
        }

        private void SourcePathBOX_DragDrop(object sender, DragEventArgs e)
        {
            SourcePathBOX.ForeColor = Color.Black;
            SourcePathBOX.Text = ((Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
            string text = SourcePathBOX.Text;
            if (MDK_Display_Info(text))
            {
                FileName = text;
                vsProj.KeilProjFullName = text;
                return;
            }
            SourcePathBOX.Text = "Double-click or drag and drop The MDK the Project File to here";
            SourcePathBOX.ForeColor = Color.Gray;
            FileName = "";
            toolStripButton_CreateVSProj.Enabled = false;
        }

        private bool MDK_Display_Info(string DocName)
        {
            if (!vsProj.MDK_CheckProject())
            {
                MessageBox.Show("This File Is Not Keil uVision Project File!!", "Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
                return false;
            }
            bool result;
            try
            {
                string[] array = vsProj.MDK_TargetRead();
                TargetListBox_Add(array);
                vsProj.IncludePath = vsProj.MDK_IncludePathRead(array[0]);
                //NMakePreprocessorDefinitions = vsProj.MDK_DefineRead(array[0]);
                string[] array2 = vsProj.MDK_GroupRead(array[0]);
                GroupListBox_Add(array2);
                string[] items = vsProj.MDK_SrcRead(array[0], array2[0]);
                SrcFileBox_Add(items);
                string[] str = vsProj.MDK_TargetStatusRead(array[0]);
                TargetStatusBox_Add(str);
                toolStripButton_CreateVSProj.Enabled = true;
                result = true;
            }
            catch
            {
                MessageBox.Show("Read File Error! Maybe it is't MDK Project File", "Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
                result = false;
            }
            return result;
        }

        private void CreateButton_Click(object sender, EventArgs e)
        {
            string vsProjPath = vsProj.Convert();
            if (vsProjPath=="")
            {
                return;
            }
            MessageBox.Show("The Visual Studio Project Creat Complete!");
            Process.Start(vsProjPath);
        }

        private void tsmi_Option_Click(object sender, EventArgs e)
        {
            Option OptionForm = new Option();
            OptionForm.ShowDialog();
        }

        private void btn_OpenSorce_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.DefaultExt = "uvproj";
            openFileDialog.Filter = "MDK Project File (*.uvprojx;*.uvproj)|*.uvprojx;*.uvproj|All files (*.*)|*.*";
            if (openFileDialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            string fileName = openFileDialog.FileName;

            OpenMDKProjFile(fileName);
        }

        private void ToolStripMenuItem_OpenFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.DefaultExt = "uvproj";
            openFileDialog.Filter = "MDK Project File (*.uvprojx;*.uvproj)|*.uvprojx;*.uvproj|All files (*.*)|*.*";
            if (openFileDialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            string fileName = openFileDialog.FileName;

            OpenMDKProjFile(fileName);
        }

        private void ToolStripMenuItem_Exit_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void toolStripButton_OpenFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.DefaultExt = "uvproj";
            openFileDialog.Filter = "MDK Project File (*.uvprojx;*.uvproj)|*.uvprojx;*.uvproj|All files (*.*)|*.*";
            if (openFileDialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            string fileName = openFileDialog.FileName;

            OpenMDKProjFile(fileName);
        }

        private void toolStripButton_CreateVSProj_Click(object sender, EventArgs e)
        {
            string vsProjPath = vsProj.Convert();
            if (vsProjPath == "")
            {
                return;
            }
            MessageBox.Show("The Visual Studio Project Creat Complete!");
            Process.Start(vsProjPath);
        }



    }
}
